package com.share.example.stock.schedule;

import com.share.common.utils.DateExtUtil;
import com.share.common.utils.DateUtil;
import com.share.example.stock.model.db.ScheduleLog;
import com.share.example.stock.model.db.StockMinPrice;
import com.share.example.stock.service.IScheduleLogService;
import com.share.example.stock.service.IStockMinPriceService;
import com.share.example.stock.service.ITradeDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 分时数据下载
 */
@Slf4j
@Component
public class S30_Down_stock_min {
    private String className = this.getClass().getSimpleName();
    @Autowired
    private IScheduleLogService iScheduleLogService;

    @Autowired
    private IStockMinPriceService iStockMinPriceService;

    @Autowired
    private ITradeDateService iTradeDateService;

    @Scheduled(cron = "0 0/5 9-15 * * ?")
    public void dowm_stock_min_nimitor() {
        Date date = new Date();
        //对时间再次细分
        int intDate = DateExtUtil.getTimeInt(date);
        if ((intDate > 93100 && intDate < 113500) || (intDate > 130100 && intDate < 151000)) {
            //可以使用的时间
        } else {
            log.info("不满足时间直接退出");
            return;
        }

        //周日直接跳过
        if (!iTradeDateService.isTradeDay(date)) {
            log.info("非交易日直接退出");
            return;
        }
        iStockMinPriceService.dowm_stock_min_monitor();
    }

    //23点执行一次是为了数据补漏,延迟
    @Scheduled(cron = "0 5/15 9-15 * * ?")
    public void dowm_stock_cur_min() {
        Date date = new Date();
        //对时间再次细分
        int intDate = DateExtUtil.getTimeInt(date);
        if ((intDate > 93300 && intDate < 115000) || (intDate > 131000 && intDate < 152000)) {
            //可以使用的时间
        } else {
            log.info("不满足时间直接退出");
            return;
        }

        //周日直接跳过
        if (!iTradeDateService.isTradeDay(date)) {
            log.info("非交易日直接退出");
            return;
        }

        String jobCode = className + "_" + "dowm_stock_cur_min";

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始下载数据库
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            List<StockMinPrice> list = iStockMinPriceService.downMin();
            upLog.setStatus("1");
            upLog.setRemark("数据条数=" + (list == null ? 0 : list.size()));
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("下载失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }

    //删除数据
    @Scheduled(cron = "0 0 5 * * ?")
    public void delete_min_date() {
        //周日直接跳过
        Date date = new Date();
        if (!iTradeDateService.isTradeDay(date)) {
            log.info("非交易日直接退出");
            return;
        }

        String jobCode = className + "_" + "delete_min_date";

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始下载数据库
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            String strDate = DateUtil.getDate(date);
            String histDay = iTradeDateService.getHistoryDay(strDate, -30);
            int count = iStockMinPriceService.deleteHistoryByDay(histDay);
            upLog.setStatus("1");
            upLog.setRemark("删除数据条数=" + count);
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("下载失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }

}
